package com.itheima.api;

import cn.hutool.core.convert.Convert;
import com.itheima.tanhua.api.mongo.RecommendApi;
import com.itheima.tanhua.pojo.db.UserInfo;
import com.itheima.tanhua.pojo.mongo.RecommendUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author 芝士雪豹
 * @create 2022/9/22/16:24
 */
@DubboService
public class RecommendServiceApi implements RecommendApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser todayBest(Long id) {
        //翻译:根据id(用户id)找到mongo里面的toUserId 并且 根据score进行降序 desc排序 得到一个数据(limit1)=最大的缘分值(SCORE)
        Query query = Query.query(Criteria.where("toUserId").is(id))
                .with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    @Override
    public List<RecommendUser> findById(Long id) {
        Query query = Query.query(Criteria.where("toUserId").is(id))
                .with(Sort.by(Sort.Order.desc("score")));
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        return recommendUsers;
    }

    @Override
    public Integer queruserId(Long userId, Long id) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("toUserId").is(id));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        Long score = recommendUser.getScore();
        return Convert.toInt(score);
    }
}
